package com.neusoft.neuiotms.dcam.fixed.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.neuiotms.dcam.common.repository.ManufacturerRepository;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Stock;
import com.neusoft.neuiotms.dcam.fixed.repository.GoodsFileRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StockRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.StorehouseRepository;
import com.neusoft.neuiotms.dcam.fixed.vo.StockExportVO;
import com.neusoft.neuiotms.dcam.fixed.vo.StockQueryExportVO;

@Component
public class StockConverter extends AbstractConverter<Stock, StockDTO> {

    @Autowired
    private StockRepository stockRepository;

    @Autowired
    private ManufacturerRepository manufacturerRepository;

    @Autowired
    private StorehouseRepository storehouseRepository;

    @Autowired
    private GoodsFileRepository goodsFileRepository;

    @Override
    public Stock toModel(final StockDTO dto) {
        final Stock model;
        if (dto.isNew()) {
            model = new Stock();
        } else {
            model = this.stockRepository.getOne(dto.getId());
        }

        model.setBatch(dto.getBatch());
        model.setApplyAssetTypeName(dto.getApplyAssetTypeName());
        model.setNumber(dto.getNumber());
        model.setUnitPrice(dto.getUnitPrice());

        if (null != dto.getGoodsFileId()) {
            try {
                model.setGoodsFile(this.goodsFileRepository.getOne(dto.getGoodsFileId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getGoodsFileId() });
            }
        }
        if (null != dto.getManufacturerId()) {
            try {
                model.setManufacturer(this.manufacturerRepository.getOne(dto.getManufacturerId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getManufacturerId() });
            }
        }
        if (null != dto.getStorehouseId()) {
            try {
                model.setStorehouse(this.storehouseRepository.getOne(dto.getStorehouseId()));
            } catch (final Exception e) {
                throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {dto.getStorehouseId() });
            }
        }

        model.setCreateTime(dto.getCreateTime());
        model.setUpdateTime(dto.getUpdateTime());
        return model;
    }

    @Override
    public StockDTO toDTO(final Stock model, final boolean forListView) {
        final StockDTO dto = new StockDTO();

        dto.setId(model.getId());
        dto.setNumber(model.getNumber());
        dto.setUnitPrice(model.getUnitPrice());
        dto.setBatch(model.getBatch());

        if (model.getGoodsFile() != null) {//货品
            dto.setGoodsFileId(model.getGoodsFile().getId());
            dto.setGoodsFileName(model.getGoodsFile().getName());
            dto.setDescription(model.getGoodsFile().getDescription());
            dto.setSpareTypeCode(model.getGoodsFile().getSpareType().getCode());
            dto.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
            dto.setSpecification(model.getGoodsFile().getSpecification());
            dto.setApplyAssetTypeName(model.getGoodsFile().getSpareType().getParent().getName());
        }
        if (model.getManufacturer() != null) {//厂商
            dto.setManufacturerId(model.getManufacturer().getId());
            dto.setManufacturerName(model.getManufacturer().getName());
        }
        if (model.getStorehouse() != null) {//仓库
            dto.setStorehouseId(model.getStorehouse().getId());
            dto.setStorehouseName(model.getStorehouse().getName());
        }

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

    public StockExportVO toVO(final Stock model) {
        final StockExportVO vo = new StockExportVO();
        vo.setNumber(model.getNumber());
        vo.setUnitPrice(model.getUnitPrice());
        vo.setApplyAssetTypeName(model.getApplyAssetTypeName());
        vo.setBatch(model.getBatch());

        if (model.getGoodsFile() != null) {//货品
            vo.setGoodsFileName(model.getGoodsFile().getName());
            vo.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
            vo.setSpecification(model.getGoodsFile().getSpecification());
            vo.setApplyAssetTypeName(model.getGoodsFile().getSpareType().getParent().getName());
        }
        if (model.getManufacturer() != null) {//厂商
            vo.setManufacturerName(model.getManufacturer().getName());
        }
        if (model.getStorehouse() != null) {//库房仓库
            vo.setStorehouseName(model.getStorehouse().getName());
        }

        vo.setCreateTime(model.getCreateTime());
        vo.setUpdateTime(model.getUpdateTime());

        return vo;
    }

    public StockQueryExportVO toStockQueryVO(final Stock model) {
        final StockQueryExportVO vo = new StockQueryExportVO();
        vo.setNumber(model.getNumber());
        vo.setUnitPrice(model.getUnitPrice());
        vo.setApplyAssetTypeName(model.getApplyAssetTypeName());
        vo.setBatch(model.getBatch());

        if (model.getGoodsFile() != null) {//货品
            vo.setGoodsFileName(model.getGoodsFile().getName());
            vo.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
            vo.setSpecification(model.getGoodsFile().getSpecification());
            vo.setApplyAssetTypeName(model.getGoodsFile().getSpareType().getParent().getName());
            if (model.getGoodsFile().getSpareType() != null) {
                vo.setSpareTypeName(model.getGoodsFile().getSpareType().getName());
            }
        }
        if (model.getManufacturer() != null) {//厂商
            vo.setManufacturerName(model.getManufacturer().getName());
        }
        if (model.getStorehouse() != null) {//库房仓库
            vo.setStorehouseName(model.getStorehouse().getName());
        }

        vo.setCreateTime(model.getCreateTime());
        vo.setUpdateTime(model.getUpdateTime());

        return vo;
    }

}
